<?php
/* SVN FILE: $Id: app_controller.php 6311 2008-01-02 06:33:52Z phpnut $ */
/**
 * Short description for file.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 6311 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 23:33:52 -0700 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.cake
 */
class AppController extends Controller {

	public $components = array('Acl', 'Auth', 'Cookie', 'Session');
	
	var $helpers = array('Javascript', 'Html', 'Form', 'Session');
	
	var $permissions = array();
	
	/**
     * username of logged in user. 
     *
     */
	public $loggedUser = null;

		
//	function beforeFilter(){
//		$this->Auth->loginAction = array ('controller' => 'users', 'action' => 'login' );
//		$this->Auth->loginRedirect = array ('controller' => 'apartments', 'action' => 'index' );
//		$this->Auth->logoutRedirect = array ('controller' => 'users', 'action' => 'login' );
//		$this->Auth->loginError = 'The information you\'ve entered is incorrect.';
//		$this->Auth->logoutError = 'You\'ve successfully logged out.';
//	}

	function beforeRender(){
		$controllerName = strtolower($this->name);
		$this->__setLoggedUserValues();
	    $this->__buildPermissions();
	    if(isset($this->permissions[$controllerName]) && false == $this->permissions[$controllerName]){
	    	die($controllerName); 
	    	$this->Auth->logout();
	    	
	    }
	}

	function __buildPermissions(){
		$all = $this->Acl->Aco->find('all');
		foreach ($all as $aco_alias){
			$this->permissions[$aco_alias['Aco']['alias']]['*'] = $this->Acl->check( $this->loggedUser, $aco_alias['Aco']['alias'] );
			$this->permissions[$aco_alias['Aco']['alias']]['create'] = $this->Acl->check( $this->loggedUser, $aco_alias['Aco']['alias'], 'create');
			$this->permissions[$aco_alias['Aco']['alias']]['read'] = $this->Acl->check( $this->loggedUser, $aco_alias['Aco']['alias'], 'read');
			$this->permissions[$aco_alias['Aco']['alias']]['update'] = $this->Acl->check( $this->loggedUser, $aco_alias['Aco']['alias'], 'update');
			$this->permissions[$aco_alias['Aco']['alias']]['delete'] = $this->Acl->check( $this->loggedUser, $aco_alias['Aco']['alias'], 'delete');
		}
		$this->set('permissions', $this->permissions);
	}
	
    /**
     * Sets a value for current logged user that is easily accessed by rest of application.
     * @returns boolean TRUE if there is a logged user FALSE if no user is logged in.
     */
    function __setLoggedUserValues(){
	    $user = null;
		if( $user = $this->Auth->user() ){
            $this->loggedUser = $user[$this->Auth->userModel][$this->Auth->fields['username']];
            $this->set ( 'username', $this->loggedUser );
			return TRUE;
		} else {
		    return FALSE;
		}
	}
	

   function __($value){
   		$dic =& ClassRegistry::init('Dictionary');
		$lang = (!empty($this->params['language']) ) ? $this->params['language'] : 'rus';
   		return $dic->__($value, $lang);
   }
}
?>